#include "Scuba.h"
#include "Sonar.h"

#include "ArgumentParser.h"
#include "Helper.h"

#include <string>
#include <iostream>
#include <conio.h>

using namespace std;

CArgumentParser* gpxArgumentParser;
string gsTarget = "./test.c";

Scuba xScubaDiver;
Sonar xSonar;
SonarOptions xSonarOptions;

int parseCommandLine(int argc, char**argv)
{
	gpxArgumentParser=new CArgumentParser("ksecybqh");

	gpxArgumentParser->m_Helper->fillProgramName("nSonar");
	gpxArgumentParser->m_Helper->fillAuthor("SonOfDelphi");
	gpxArgumentParser->m_Helper->fillSummary("Neptuner Test/Debug Assistant");
	gpxArgumentParser->m_Helper->fillUsageLine("[OPTIONS] <SourceFile>");
	gpxArgumentParser->fillHelpForOption('k',"Display parsed tokens");
	gpxArgumentParser->fillHelpForOption('s',"Display scope with symbol-table");
	gpxArgumentParser->fillHelpForOption('e',"Echo parsed contents");
	gpxArgumentParser->fillHelpForOption('c',"Display scope-change" );
	gpxArgumentParser->fillHelpForOption('y',"File Parse Summary: default" );
	gpxArgumentParser->fillHelpForOption('b',"Bare (No title row)");
	gpxArgumentParser->fillHelpForOption('q',"Quiet mode");
	gpxArgumentParser->fillHelpForOption('h',"Displays this help message");

	gpxArgumentParser->m_Helper->fillYear("2009");
	gpxArgumentParser->m_Helper->fillOwner("The Oraclique");
	gpxArgumentParser->m_Helper->fillTailMessage("Part of Neptuner Code-Management Suite.\nBuild:" __DATE__"/"__TIME__  "\nThis program comes with absolutely no warranty.");

	int nParseEndPos = gpxArgumentParser->parse(argc,argv);
	
	if(!gpxArgumentParser->wasParseSuccessful())
	{
		gpxArgumentParser->m_Helper->displayHelp();
		delete gpxArgumentParser;
		cerr<<"Invalid option supplied"<<endl;
		return -1;
	}

	xSonarOptions.m_bNoHeading		= gpxArgumentParser->wasOptionSupplied('b');
	xSonarOptions.m_bModeQuiet		= gpxArgumentParser->wasOptionSupplied('q');
	xSonarOptions.m_bEchoWorkspace	=gpxArgumentParser->wasOptionSupplied('e');
	xSonarOptions.m_bScopeChanges	 = gpxArgumentParser->wasOptionSupplied('c');

	bool bValue;
	bool bOneSupplied = false;

	bValue = gpxArgumentParser->wasOptionSupplied('k');
	xSonarOptions.m_bTokens			= bValue;
	bOneSupplied |= bValue;

	bValue = gpxArgumentParser->wasOptionSupplied('s');
	xSonarOptions.m_bSymbolTables	=bValue;
	bOneSupplied |= bValue;

	bValue = gpxArgumentParser->wasOptionSupplied('y');
	xSonarOptions.m_bFileSummary	= bValue;
	bOneSupplied |= bValue;

	if(!bOneSupplied)
		xSonarOptions.m_bFileSummary = true;

	return nParseEndPos+1;
}

int main(int argc, char**argv)
{
	int nProgArgIndex = parseCommandLine(argc, argv);
	if(nProgArgIndex<0)
	{
		return 1;
	}

	if(gpxArgumentParser->wasOptionSupplied('h'))
	{
		gpxArgumentParser->m_Helper->displayHelp();
		delete gpxArgumentParser;
		return 0;
	}

	if(nProgArgIndex<argc)
	{
			gsTarget=argv[nProgArgIndex];
	}
	else
	{
		cerr<<"Attempting to proceed with default test file: "<<gsTarget<<endl;
	}

	if(xSonarOptions.m_bEchoWorkspace) xSonar.m_bLiveTokenDisplay = true;
	if(xSonarOptions.m_bScopeChanges)  xSonar.m_bScopeChangeDisplay = true;

	if(!xSonarOptions.m_bModeQuiet)
		cerr<<"\n<<"<<gsTarget<<">>"<< " FILE INFORMATION\n\n";

    xScubaDiver.setObserver(&xSonar);
	xScubaDiver.dive(gsTarget);

	xSonar.print(&xSonarOptions);

	if(!xSonarOptions.m_bModeQuiet)
	    cerr<<"\n<<"<<gsTarget<<">>"<< " END OF FILE INFORMATION\n";

	if(argc==1)
	{
		cerr<<"Press any key to continue..";
		_getch();
	}

    return 0;
}


